package org.hxzon.workeasy.pages.file;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.hxzon.workeasy.model.EasyFile;
import org.hxzon.workeasy.pages.base.MyBasePage;
import org.hxzon.workeasy.permissions.FileCRPermissions;
import org.hxzon.workeasy.service.EasyTagService;
import org.slf4j.Logger;

public class TagFile extends MyBasePage {
    @Inject
    private Logger logger;
    @Inject
    private EasyTagService tagSer;
    @Property
    private List<EasyFile> list;
    @Property
    private String tagString;
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    public Object initialize(String tagString, long size, long first) {
        this.tagString = tagString;
        page.setCurrentPage(first);
        page.setPageSize(size);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String tagString, long size, long first) {
        new FileCRPermissions().check(null, getVisitor());
        initialize(tagString, size, first);
        return null;
    }

    @SetupRender
    Object display() {
        list = tagSer.findFileByTag(tagString, page.getCurrentRow(), page.getPageSize());
        page.setRowCount(tagSer.countFileByTag(tagString));
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { tagString, page.getPageSize(), page.getCurrentPage() };
    }

}
